﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.Menu;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.System;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Menu.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Menu
{
    public class GetMenuConfigService : ServiceBase, IGetMenuConfigService
    {
        protected readonly IMenuRepository menuRepository;

        public GetMenuConfigService(ILogger<ServiceBase> logger, IOptions<WmsSettings> options, IMapper mapper, IMenuRepository menuRepository) : base(logger, options, mapper)
        {
            this.menuRepository = menuRepository;
        }

        public async Task<ActionResult<MenuConfigDO>> GetMenuConfig(string menuId, UserInfo userInfo)
        {
            var result = new ActionResult<MenuConfigDO>("获取菜单配置");
            try
            {
                if (string.IsNullOrWhiteSpace(menuId))
                    return result.ArgumentError("菜单ID不能为空");

                if (menuId == "0")
                {
                    result.Data = new MenuConfigDO()
                    {
                        Id = 0,
                        NameCN = "顶级菜单",
                        NameEN = "Top Menu",
                        SortNumber = 0,
                        Icon = "home.png",
                        TypeCode = "MenuType_Menu",
                        MenuPositionCode = "MenuPosition_Menu",
                        Url = "",
                        Sql = "",
                    };
                }
                else
                {
                    var menuPO = await menuRepository.FindById<MenuPO>(Convert.ToInt64(menuId));
                    if (menuPO == null)
                        return result.DbError($"ID:{menuId},菜单数据未找到");

                    result.Data = mapper.Map<MenuConfigDO>(menuPO);
                }
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
